package org.hospital.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.hospital.entity.Employee;
import org.hospital.entity.Log;
import org.hospital.entity.MedicalRecord;
import org.hospital.entity.TreatInfo;
import org.hospital.service.LogService;
import org.hospital.service.MedicalCardService;
import org.hospital.service.PermissionService;
import org.hospital.util.Constant;
import org.hospital.util.DateUtil;
import org.hospital.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.WebRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping("medicalCardController")
@SessionAttributes(value = "employee", types = Employee.class)
public class MedicalCardController {

	@Autowired
	private PermissionService pmService;
	@Autowired
	private MedicalCardService mcService;
	@Autowired
	private LogService lService;
	
	@ResponseBody
	@RequestMapping(value="/getClientTreatInfo",produces="text/json;charset=utf-8")
	public String getClientTreatInfo(WebRequest req,HttpSession session) {
		try {
			//获取数据
			int clientId = Integer.parseInt(req.getParameter("clientId"), 10);
			JSONArray result = mcService.getClientTreatInfo(clientId);
			if(result.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", result);
			
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/createTreatInfo", produces = "text/json;charset=utf-8")
	public String createTreatInfo(WebRequest req, HttpSession session) {
		// 检测用户权限
		Employee operator = (Employee) session.getAttribute("employee");

		if (operator == null) {
			return StringUtil.setResult(405, "请重新登录", "");
		}

		if (!pmService.isPermisionEnough(operator.getPositionId(),
				Constant.CREATE_TREATMENT_BILL.getName(), 1)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		try {
			//获取参数
			int projectId = Integer.parseInt(req.getParameter("projectId"), 10);
			int clientId = Integer.parseInt(req.getParameter("clientId"),10);
			int excutorId = Integer.parseInt(req.getParameter("excutorId"), 10);
			
			//TODO 验证有效性
			
			//插入数据库
			TreatInfo ti = mcService.createTreatInfo(projectId, clientId, excutorId);
			
			if(ti == null) {
				return StringUtil.setResult(501, "数据库操作失败", "");
			}
			
			Log log = new Log();
			log.setEmployeeId(operator.getEmployeeId());
			log.setContent(JSON.toJSONString(ti));
			log.setOperate(Constant.CREATE_TREATINFO.getName());
			log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
			lService.addEntity(log);
			
			return StringUtil.setResult(200, "操作成功", JSON.toJSON(ti));
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
		
	}
	
	@ResponseBody
	@RequestMapping(value="/getMedicalCardInfo",produces="text/json;charset=utf-8")
	public String getMedicalCardInfo(WebRequest req,HttpSession session){
		
		try {
			//获取数据
			int clientId = Integer.parseInt(req.getParameter("clientId"), 10);
			JSONArray resultJA = mcService.getMedicalCardInfo(clientId);
			if(resultJA.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", resultJA);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}
	
	
	/**
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/createMedicalRecord",produces="text/json;charset=utf-8")
	public String createMedicalRecord(WebRequest req,HttpSession session){
		
		try {
			int clientId = Integer.parseInt(req.getParameter("clientId"), 10);
			int employeeId = Integer.parseInt(req.getParameter("employeeId"),10);
			int departmentId = Integer.parseInt(req.getParameter("departmentId"),10);
			int clinicId = Integer.parseInt(req.getParameter("clinicId"), 10);
			String diseaseName = req.getParameter("diseaseName");
			
			MedicalRecord mr = new MedicalRecord();
			mr.setClientId(clientId);
			mr.setClinicId(clinicId);
			mr.setDepartmentId(departmentId);
			mr.setDiseaseName(diseaseName);
			mr.setEmployeeId(employeeId);
			mr.setDatetime(new Timestamp(DateUtil.getNowLong()));
			
			mr = mcService.creatrMedicalRecord(mr);
			if(mr == null) {
				return StringUtil.setResult(501, "插入错误", "");
			}
			return StringUtil.setResult(200, "操作成功", mr);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/getClientMedicalRecord",produces="text/json;charset=utf-8")
	public String getClientMedicalRecord(WebRequest req,HttpSession session){
		try {
			int clientId = Integer.parseInt(req.getParameter("clientId"), 10);
			
			JSONArray resultJA = mcService.getClientMedicalRecord(clientId);
			
			if(resultJA==null || resultJA.size() == 0) {
				return StringUtil.setResult(414, "数据为空", "");
			}
			return StringUtil.setResult(200, "操作成功", resultJA);
		} catch (Exception e) {
			e.printStackTrace();
			return StringUtil.setResult(403, "参数格式有误", "");
		}
	}
	
}












